package com.free.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * <p>
 * Title: Eclipse Plugin Development
 * </p>
 * <p>
 * Description: Free download
 * </p>
 * <p>
 * mail: ganshm@gmail.com
 * </p>
 */

public class ChooseFontDialogExample
{
    private Font font;
    
    private Color color;
    
    /**
     * Runs the application
     */
    public void run()
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Font Chooser");
        createContents(shell);
        shell.pack();
        shell.open();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        // Dispose the font and color we created
        if (font != null)
            font.dispose();
        if (color != null)
            color.dispose();
        
        display.dispose();
    }
    
    /**
     * Creates the window contents
     * 
     * @param shell the parent shell
     */
    private void createContents(final Shell shell)
    {
        shell.setLayout(new GridLayout(2, false));
        
        final Label fontLabel = new Label(shell, SWT.NONE);
        fontLabel.setText("The selected font");
        
        Button button = new Button(shell, SWT.PUSH);
        button.setText("Font...");
        button.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent event)
            {
                // 新建字体对话框
                FontDialog dlg = new FontDialog(shell);
                // 设置初始字体和颜色
                if (font != null)
                    dlg.setFontList(fontLabel.getFont().getFontData());
                if (color != null)
                    dlg.setRGB(color.getRGB());
                // 打开对话框
                if (dlg.open() != null)
                {
                    if (font != null)
                        font.dispose();
                    if (color != null)
                        color.dispose();
                    
                    // 新建字体对象
                    font = new Font(shell.getDisplay(), dlg.getFontList());
                    // 设置标签的字体
                    fontLabel.setFont(font);
                    
                    // 新建颜色对象
                    color = new Color(shell.getDisplay(), dlg.getRGB());
                    // 设置标签颜色
                    fontLabel.setForeground(color);
                    
                    // Call pack() to resize the window to fit the new font
                    shell.pack();
                }
            }
        });
    }
    
    /**
     * The application entry point
     * 
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        new ChooseFontDialogExample().run();
    }
}
